package org.will.demo.domain;

import org.will.ddd.domain.AggregateRoot;
import org.will.demo.events.MQDemoCreateEvent;
import org.will.demo.events.MQDemoUpdateEvent;

/**
 * Created by EdifierWill on 2016/8/5.
 */
public class MQDemo extends AggregateRoot<Long> {
    public final String _ename;
    public final Double _price;
    public final Double _inventory;

    public MQDemo(Long id,String ename,Double price,Double inventory) {
        super(id);
        this._ename = ename;
        this._price = price;
        this._inventory = inventory;
    }

    public static MQDemo create(Long id,String ename,Double price,Double inventory)
    {
        MQDemo mqDemo = new MQDemo(id,ename,price,inventory);
        try {
            mqDemo.ApplyEvent(new MQDemoCreateEvent(id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mqDemo;
    }

    public MQDemo Update(String name,Double price,Double inventory)
    {
        MQDemo mqDemo = new MQDemo(this._id,name,price,inventory);
        mqDemo.setVersion(this.Version());//由于采用当前方式(Immutable 聚合都是通过New重新生成,故需要设定其Version),重新设置Version用于事件是否重复判断
        try {
            mqDemo.ApplyEvent(new MQDemoUpdateEvent(this._id));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mqDemo;
    }
}
